
class List {
    constructor() {
        this.url = "http://localhost:3000/api";
        this.cont = document.querySelector("#list");
        this.addEvent();
    }
    addEvent() {
        const that = this;
        this.cont.addEventListener("mousedown",function(eve){
            const e = eve || window.event;
            const target = e.target || e.srcElement;
            if (target.className === "buyBtn"){
                target.style.backgroundColor="red";
                target.addEventListener("mouseup",function(eve){
                    const e = eve || window.event;
                    const target = e.target || e.srcElement;
                    target.style.backgroundColor="";
                })
                
            }
        })
        this.cont.addEventListener('click', function (eve) {
            const e = eve || window.event;
            const target = e.target || e.srcElement;
            if (target.className === "buyBtn") {
                console.log("+1")
                that.id = target.parentNode.getAttribute("index");
                that.setLocal();
            }
        })
    }
    setLocal() {
        const goods = localStorage.getItem("goodsList") ? JSON.parse(localStorage.getItem("goodsList")) : [];
        if (goods.length < 1) {
            goods.push({
                id: this.id,
                num: 1
            })
        } else {
            let i = 0;
            const flag = goods.some((val, idx) => {
                i = idx;
                return val.id === this.id
            })
            if (flag) {
                goods[i].num++;
            } else {
                goods.push({
                    id: this.id,
                    num: 1
                })
            }
        }
        localStorage.setItem("goodsList", JSON.stringify(goods))
    }
}
new List();